package com.itheima.train.manager.service;

import com.alibaba.fastjson.JSON;
import com.itheima.train.manager.domain.OrderSynEs;
import com.itheima.train.manager.domain.TbRidingPlanDate;
import com.itheima.train.manager.mq.ZhzEsConsumer;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * @program: itheima-train-manager-parent
 * @description: 对es操作, 进行库存扣减
 * @author: zhanghz001
 * @create: 2021-07-06 15:29
 **/
// @Service
public class ZhzEsIndexService {
    // 创建一个日志对象
    private Logger log = LoggerFactory.getLogger(ZhzEsConsumer.class);
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    
    // 同步库存的扣减操作
    public void synStockInfo(OrderSynEs orderSynEs) throws IOException {
        //构造查询条件    
        SearchRequest searchRequest = new SearchRequest("riding_plan_date");
        
        //查询所有满足添加的数据,不然会只限制10万
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
        
        //查询条件构造细节
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("trainNum", orderSynEs.getTrainNum()));
        boolQueryBuilder.must(QueryBuilders.termQuery("trainRidingDate", orderSynEs.getRidingPlanDate()));
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.size(1000);
        searchRequest.source(sourceBuilder);
        
        //进行查询
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits searchHits = searchResponse.getHits();
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit searchHit : hits) {
            String sourceAsString = searchHit.getSourceAsString();
            TbRidingPlanDate ridingPlanDate = JSON.parseObject(sourceAsString, TbRidingPlanDate.class);
            switch (orderSynEs.getSeatNature()) {
                case "3":
                    ridingPlanDate.setFirstSeatStock(ridingPlanDate.getFirstSeatStock() - 1);
                    break;
                case "4":
                    ridingPlanDate.setSecondSeatStock(ridingPlanDate.getSecondSeatStock() - 1);
                    break;
                case "7":
                    ridingPlanDate.setHardSleeperStock(ridingPlanDate.getHardSleeperStock() - 1);
                    break;
                case "6":
                    ridingPlanDate.setSoftSleeperStock(ridingPlanDate.getSoftSleeperStock() - 1);
                    break;
                case "2":
                    ridingPlanDate.setBusSeatStock(ridingPlanDate.getBusSeatStock() - 1);
                    break;
                case "5":
                    ridingPlanDate.setHardSeatStock(ridingPlanDate.getHardSeatStock() - 1);
                    break;
                case "9":
                    ridingPlanDate.setSoftSeatStock(ridingPlanDate.getSoftSeatStock() - 1);
                    break;
            }
            
            //创建一个更新的请求对象
            
            UpdateRequest updateRequest =
                    new UpdateRequest("riding_plan_date", searchHit.getId())
                            .doc(JSON.toJSONString(ridingPlanDate), XContentType.JSON);
            restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        }
    }
}
